﻿using Sustainalytics.Auth.Claims;
using Sustainalytics.ClientDW.DataAccessLayer;
using Sustainalytics.ClientDW.Entities.Universes;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Claims;

namespace Sustainalytics.Universes.BusinessLogic
{
    public class UniversesManagement : IUniversesManagement
    {
        private IUniversesRepository _universesRepository = null;

        private UniversesManagement()
        {
        }

        public static IUniversesManagement GetInstance(IUniversesRepository universesRepository)
        {
            return new UniversesManagement { _universesRepository = universesRepository };
        }

        public Tuple<Stream, Guid> UploadFile(Guid id, string fileName, string contentType)
        {
            return _universesRepository.UploadFile(id, fileName, contentType);
        }

        public bool IsProcessingScheduled()
        {
            return _universesRepository.IsProcessingScheduled();
        }

        public void AddUploadLogs(UploadLog uploadLog)
        {
            _universesRepository.AddUploadLogs(uploadLog);
        }

        public List<UploadErrorLog> GetUploadErrorLogs()
        {
            return _universesRepository.GetUploadErrorLogs();
        }

        public List<Claim> GetFeatureUniveseClaims(Guid accountId)
        {
            var featureUniverses = _universesRepository.GetFeatureUniveseByAccountId(accountId);

            return featureUniverses
                .Select(featureUniv => new Claim(CustomClaimTypes.UniverseFeature, string.Format("{0},{1}", (int)featureUniv.Feature, string.Join(",", featureUniv.UniverseIds))))
                .ToList();
        }

        public List<Universe> GetUniversesByAccountId(Guid accoundId)
        {
            return _universesRepository.GetUniversesByAccountId(accoundId);
        }

        public List<Universe> GetUniversesByNameForAccountId(Guid accoundId, string univNameLike)
        {
            return _universesRepository.GetUniversesEntitiesByNameForAccountId(accoundId, univNameLike);
        }
    }
}
